package com.huiquan.framework.utils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Component;

@Component
public class GetListUtil {
	public static Integer LINES_NO_PER_PAGE = 20;
	


	/**
	 * 根据总数和开始页码计算列表中需要的页码相关数据
	 * 
	 * @param totalSize
	 *            列表总数
	 * @param start
	 *            开始页码
	 * @return
	 */
	public static Map<String, Integer> getPageNoMap(Integer totalSize, Integer start) {
		return getPageNoMap(totalSize, start, 20, 200);
	}

	public static Map<String, Integer> getPageNoMap(Integer totalSize, Integer start, Integer maxPage) {
		return getPageNoMap(totalSize, start, 20, maxPage);
	}

	public static Map<String, Integer> getPageNoMap(Integer totalSize, Integer start, Integer countPerPage,
			Integer maxPage) {
		int endPage = totalSize / countPerPage;
		if (totalSize % countPerPage != 0)
			endPage++;

		if (endPage >= maxPage && maxPage > -1)
			endPage = maxPage;

		if (endPage < 1) {
			endPage = 1;
		}

		if (start > endPage)
			start = endPage;

		if (start < 1)
			start = 1;

		int page = countPerPage;
		int startIndex = (start - 1) * page;

		Map<String, Integer> noMap = new HashMap<>();
		noMap.put("endPage", endPage);
		noMap.put("start", start);
		noMap.put("startIndex", startIndex);
		noMap.put("linesNoPerPage", countPerPage);

		return noMap;
	}

	/**
	 * 根据列表获取后相关数据组装map
	 * 
	 * @param status
	 *            页面状态码
	 * @param totalSize
	 *            列表总数
	 * @param start
	 *            开始页码
	 * @param startIndex
	 *            开始数量
	 * @param endPage
	 *            结束页码
	 * @param list
	 *            列表数据
	 * @param keyword
	 *            查询关键字
	 * @return
	 */
	public static <T> Map<String, Object> getReturnMap(int status, int totalSize, Integer start, int startIndex,
			int endPage, List<T> list, Map<String, String> ortherParam) {
		Map<String, Object> map = new HashMap<>();
		map.put("status", status);
		map.put("size", totalSize);
		map.put("startIndex", startIndex + 1);
		map.put("endIndex", startIndex + list.size());
		map.put("list", list);
		map.put("currentPage", start);
		map.put("prevPage", (start - 1) < 1 ? 1 : (start - 1));
		map.put("nextPage", (start + 1) > endPage ? endPage : (start + 1));
		map.put("startPage", 1);
		map.put("endPage", endPage);
		map.put("showStartPage", (start - 3) < 1 ? 1 : (start - 3));
		map.put("showEndPage", (start + 3) > endPage ? endPage : (start + 3));
		if (ortherParam != null && ortherParam.size() > 0) {
			map.putAll(ortherParam);
		}
		return map;
	}

	/**
	 * 根据列表获取后相关数据组装map
	 * 
	 * @param totalSize
	 *            列表总数
	 * @param start
	 *            开始页码
	 * @param startIndex
	 *            开始数量
	 * @param endPage
	 *            结束页码
	 * @param list
	 *            列表数据
	 * @param keyword
	 *            查询关键字
	 * @return
	 */
	public static <T> Map<String, Object> getReturnMap2(Integer totalSize, Integer start, Integer startIndex,
			Integer endPage, List<T> list, Map<String, String> ortherParam) {
		Map<String, Object> map = new HashMap<>();
		map.put("size", totalSize);
		map.put("startIndex", startIndex + 1);
		map.put("endIndex", startIndex + list.size());
		map.put("list", list);
		map.put("currentPage", start);
		map.put("prevPage", (start - 1) < 1 ? 1 : (start - 1));
		map.put("nextPage", (start + 1) > endPage ? endPage : (start + 1));
		map.put("startPage", 1);
		map.put("endPage", endPage);
		map.put("showStartPage", (start - 3) < 1 ? 1 : (start - 3));
		map.put("showEndPage", (start + 3) > endPage ? endPage : (start + 3));
		if (ortherParam != null && ortherParam.size() > 0) {
			map.putAll(ortherParam);
		}
		return map;
	}

	public static Map<String, Integer> getPageNoParam(int totalSize, String startStr) {
		int start = 1;
		if (startStr != null && !startStr.isEmpty()) {
			start = Integer.parseInt(startStr);
		}

		return getPageNoMap(totalSize, start, 20, -1);
	}

	public static Map<String, Integer> getPageNoParam(int totalSize, String startStr, int countPerPage) {
		int start = 1;
		if (startStr != null && !startStr.isEmpty()) {
			start = Integer.parseInt(startStr);
		}

		return getPageNoMap(totalSize, start, countPerPage, -1);
	}

	public static Map<String, Integer> getPagingParam(int totalSize, String startStr, int countPerPage) {
		int start = 1;
		if (startStr != null && !startStr.isEmpty()) {
			start = Integer.parseInt(startStr);
		}

		int endPage = totalSize / countPerPage;
		if (totalSize % countPerPage != 0)
			endPage++;

		if (endPage < 1) {
			endPage = 1;
		}

		if (start > endPage) {
			start = endPage;
		}

		if (start < 1) {
			start = 1;
		}

		int startIndex = (start - 1) * countPerPage;

		Map<String, Integer> map = new HashMap<>();
		map.put("startIndex", startIndex);
		map.put("currentPage", start);
		map.put("prevPage", (start - 1) < 1 ? 1 : (start - 1));
		map.put("nextPage", (start + 1) > endPage ? endPage : (start + 1));
		map.put("startPage", 1);
		map.put("endPage", endPage);
		map.put("showStartPage", (start - 3) < 1 ? 1 : (start - 3));
		map.put("showEndPage", (start + 3) > endPage ? endPage : (start + 3));
		map.put("countPerPage", countPerPage);

		return map;

	}

	public static Map<String, Integer> getPagingParam(int totalSize, String startStr) {

		return getPagingParam(totalSize, startStr, 20);
	}

	public static Map<String, Integer> getPagingParam(int totalSize, String startStr, String countPerPageStr) {

		int countPerPage = 20;
		if (countPerPageStr != null && !countPerPageStr.isEmpty()) {
			countPerPage = Integer.parseInt(countPerPageStr);
		}

		return getPagingParam(totalSize, startStr, countPerPage);
	}
}
